<template>
  <div>
    <div class="n-layout-page-header">
      <n-card :bordered="false">
        <!--  这是由系统生成的CURD表格，你可以将此行注释改为表格的描述 -->
        <!--        上涨TOP50下跌TOP50主力流入TOP50主力流出TOP50-->
      </n-card>
    </div>
    <n-card :bordered="false" class="proCard">
      <n-tabs type="line" animated>
        <n-tab-pane name="top50" tab="上涨TOP50">
          <BasicTable
            :openChecked="true"
            :columns="columns"
            :request="loadTop50DataTable"
            :row-key="(row) => row.code"
            ref="actionRef"
            :actionColumn="null"
            :checked-row-keys="checkedIds"
            @update:checked-row-keys="onCheckedRow"
            @update:sorter="handleUpdateSorter"
            :scroll-x="1090"
            :resizeHeightOffset="-10000"
            :pagination="{ pageSize: 50 }"
            size="small"
          />
        </n-tab-pane>
        <n-tab-pane name="down50" tab="下跌TOP50">
          <BasicTable
            :openChecked="true"
            :columns="columns"
            :request="loadDownTopDataTable"
            :row-key="(row) => row.code"
            ref="actionRef"
            :actionColumn="null"
            :checked-row-keys="checkedIds"
            @update:checked-row-keys="onCheckedRow"
            @update:sorter="handleUpdateSorter"
            :scroll-x="1090"
            :resizeHeightOffset="-10000"
            :pagination="{ pageSize: 50 }"
            size="small"
          />
        </n-tab-pane>
        <n-tab-pane name="inflow50" tab="主力流入TOP50">
          <BasicTable
            :openChecked="true"
            :columns="columns"
            :request="loadInflow50DataTable"
            :row-key="(row) => row.code"
            ref="actionRef"
            :actionColumn="null"
            :checked-row-keys="checkedIds"
            @update:checked-row-keys="onCheckedRow"
            @update:sorter="handleUpdateSorter"
            :scroll-x="1090"
            :resizeHeightOffset="-10000"
            :pagination="{ pageSize: 50 }"
            size="small"
          />
        </n-tab-pane>
        <n-tab-pane name="output50" tab="主力流出TOP50">
          <BasicTable
            :openChecked="true"
            :columns="columns"
            :request="loadOutput50DataTable"
            :row-key="(row) => row.code"
            ref="actionRef"
            :actionColumn="null"
            :checked-row-keys="checkedIds"
            @update:checked-row-keys="onCheckedRow"
            @update:sorter="handleUpdateSorter"
            :scroll-x="1090"
            :resizeHeightOffset="-10000"
            :pagination="{ pageSize: 50 }"
            size="small"
          />
        </n-tab-pane>
      </n-tabs>
    </n-card>
    <Edit @reloadTable="reloadTable" ref="editRef" />
    <View ref="viewRef" />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { DataTableSortState, useDialog, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { usePermission } from '@/hooks/web/usePermission';
  import { List, Export, Delete } from '@/api/stockZhASpotEm';
  import { columns, schemas } from './model';
  import Edit from './edit.vue';
  import View from './view.vue';
  import { useSorter } from '@/hooks/common';

  const dialog = useDialog();
  const message = useMessage();
  const { hasPermission } = usePermission();
  const actionRef = ref();
  const searchFormRef = ref<any>({});
  const viewRef = ref();
  const editRef = ref();
  const batchDeleteDisabled = ref(true);
  const checkedIds = ref([]);

  const actionColumn = reactive({});
  const { updateSorter: handleUpdateSorter } = useSorter(reloadTable);

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  const loadTop50DataTable = async (res) => {
    const sortStatesRef = ref<DataTableSortState[]>([]);
    const sort = {
      columnKey: 'changePercent',
      order: 'descend',
      sorter: 'default',
    } as DataTableSortState;
    sortStatesRef.value.push(sort);
    return await List({
      ...searchFormRef.value?.formModel,
      ...{ sorters: sortStatesRef.value },
      ...res,
    });
  };
  const loadDownTopDataTable = async (res) => {
    const sortStatesRef = ref<DataTableSortState[]>([]);
    const sort = {
      columnKey: 'changePercent',
      order: 'ascend',
      sorter: 'default',
    } as DataTableSortState;
    sortStatesRef.value.push(sort);
    return await List({
      ...searchFormRef.value?.formModel,
      ...{ sorters: sortStatesRef.value },
      ...res,
    });
  };
  const loadOutput50DataTable = async (res) => {
    const sortStatesRef = ref<DataTableSortState[]>([]);
    const sort = {
      columnKey: 'net_inflow',
      order: 'ascend',
      sorter: 'default',
    } as DataTableSortState;
    sortStatesRef.value.push(sort);
    return await List({
      ...searchFormRef.value?.formModel,
      ...{ sorters: sortStatesRef.value },
      ...res,
    });
  };
  const loadInflow50DataTable = async (res) => {
    const sortStatesRef = ref<DataTableSortState[]>([]);
    const sort = {
      columnKey: 'net_inflow',
      order: 'descend',
      sorter: 'default',
    } as DataTableSortState;
    sortStatesRef.value.push(sort);
    return await List({
      ...searchFormRef.value?.formModel,
      ...{ sorters: sortStatesRef.value },
      ...res,
    });
  };

  function onCheckedRow(rowKeys) {
    batchDeleteDisabled.value = rowKeys.length <= 0;
    checkedIds.value = rowKeys;
  }

  function reloadTable() {
    actionRef.value.reload();
  }
</script>

<style lang="less" scoped></style>
